package com.topView2;

import com.bean.ListNode;
import com.bean.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @Author: huangzhigao
 * @Date: 2023/2/12 18:37
 */
public class leecode102 {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        LinkedList<TreeNode> list = new LinkedList<>();
        if (root == null) {
            return res;
        }
        list.add(root);
        while (!list.isEmpty()) {
            int size = list.size();
            List<Integer> level = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                TreeNode treeNode = list.removeFirst();
                level.add(treeNode.val);
                if (treeNode.left != null) {
                    list.add(treeNode.left);
                }
                if (treeNode.right != null) {
                    list.add(treeNode.right);
                }
            }
            res.add(level);
        }
        return res;
    }
}
